$J(document).ready(function(){ 
	AcLocktList.init();
}); 


function ajax(url,cb){
	$J.get(url,cb);
}

function upDown(mac){
	var btn=$J("#btn_"+mac).text();
	var updown=(btn.indexOf("上升")>-1?0:1);
	
	var url=window.baseUrl+"/api/setLock/"+mac+"/"+updown;
	var cb=function(res){
		if(res.code==200){
			alert("成功");
			$J("#btn_"+mac).text(updown?"上升":"下降");
			$J("#updown_"+mac).text(updown?"降下":"升起");
		}else{
			alert("失败："+res.msg);
		}
	};
	ajax(url,cb);
}

function getState(mac){
	
	var url=window.baseUrl+"/api/getLock/"+mac;
	var cb=function(res){
		if(res.code==200){
			var data=res.data;
			var updown=data.upDownState?"降下":"升起";
			var ele=data.eleState;
			var buzzer=data.buzzerState?"打开":"关闭";
			
			alert("电量等级："+ele+"  升降："+updown+"  蜂鸣器："+buzzer);
			$J("#btn_"+mac).text(data.upDownState?"上升":"下降");
			$J("#updown_"+mac).text(updown);
			$J("#ele_"+mac).text(ele);
			$J("#buzzer_"+mac).text(buzzer);
		}else{
			alert("失败："+res.msg);
		}
	};
	ajax(url,cb);
}




var AcLocktList={
		lockId :null,
		init:function(){
			var o=this;
			this.doAjax();
			
			//每十秒请求一次
			interval = setInterval(o.doAjax, "10000");  
		},
		//显示数据
	  doAjax:function() {
		  var o=AcLocktList;
		  var url=window.baseUrl + "/pdact/showActLockList";
			$J.ajax({
				type : 'get',
				url : url,
				dataType : 'json',
				async : true,
				cache : false,
				success : function(data) {
					if (data.code != 200) {
						if (data.msg)
							alert("数据有误")
						return;
					}
					o.showDate(data);
				},
				error : function(data) {
					$J("#msg").html(data.msg);
				}
			});
		},
		showDate:function(data){
			var html="";
			var dataList=data.data;
			if(dataList.length==0){
				html+="<tr><td colspan=9 class=\"nodata\">暂无数据</td></tr>";
				$J("#list").html(html);
				return;
			}
			for(var i in dataList){
				html+="<tr><td >"+(parseInt(i)+1)+"</id>" +
						"<td ><a target=\"_self\" href=\""+CTX+"/gateway/"+dataList[i].gatewayId+"\" >"+dataList[i].gatewayName+"</a></td>" +
						"<td >"+dataList[i].mac+"</td><td >"+dataList[i].name+"</td><td id=\"buzzer_"+dataList[i].mac+"\">";
						if(dataList[i].buzzerState==0)
		           			html+="关闭</td>";
		           		else
		           			html+="打开	</td>";
				html+="<td id=\"ele_"+dataList[i].mac+"\">"+dataList[i].eleState+"</td>" +
						"<td id=\"updown_"+dataList[i].mac+"\">";	
						if(dataList[i].upDownState==0)
							html+="升起</td><td>";
		           		else
		           			html+="降下</td><td>";
						
						if(dataList[i].isOnline)
							html+="在线</td>";
		           		else
		           			html+="离线</td>";
				html+="<td><a target=\"_self\" href=\""+CTX+"/pd/"+dataList[i].id+"/golockdetail\"><i class=\"fa fa-edit\"></i>编辑</a>&nbsp;|&nbsp;" +
						"<a target=\"_self\" id=\"btn_"+dataList[i].mac+"\" href=\"javascript:upDown("+"'"+dataList[i].mac+"'"+")\">";
				if(dataList[i].upDownState==0)
  					html+="下降</a></td></tr>";
  				else
  					html+="上升</a></td></tr>";
			}
			$J("#list").html(html);
		},
		//注册事件
		addLisen:function(){
			var o=this;
		}
}